//
//  ProblemDetailViewController.m
//  Utils
//
//  Created by enghou on 2018/4/18.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "ProblemDetailViewController.h"
#import "NetWorkerManager.h"
#import "ProblemDetailModel.h"
#import "FileItemModel.h"
#import "UIImageView+WebCache.h"
#import <AVKit/AVKit.h>
#import "PlayAudioViewController.h"
#import "MSSBrowseLocalViewController.h"
#import "WebVoicePlayerViewController.h"
@interface ProblemDetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *eventNameLabel;

@property (weak, nonatomic) IBOutlet UILabel *orgNameLabel;

@property (weak, nonatomic) IBOutlet UILabel *occurPlaceLabel;

@property (weak, nonatomic) IBOutlet UILabel *occurDateLabel;

@property (weak, nonatomic) IBOutlet UILabel *relatedCompaynLabel;

@property (weak, nonatomic) IBOutlet UILabel *relatedPersonCountLabel;

@property (weak, nonatomic) IBOutlet UILabel *manipulatorLabel;

@property (weak, nonatomic) IBOutlet UILabel *eventTypeLabel;

@property (weak, nonatomic) IBOutlet UILabel *eventScaleLabel;

@property (weak, nonatomic) IBOutlet UILabel *eventSourceLabel;

@property (weak, nonatomic) IBOutlet UILabel *eventDescLabel;

@property (weak, nonatomic) IBOutlet UILabel *suggestionLabel;

@property (weak, nonatomic) IBOutlet UILabel *forecastLabel;

@property (weak, nonatomic) IBOutlet UILabel *adjustMeasuresLabel;

@property(nonatomic,strong)NSArray *files;


@end

@implementation ProblemDetailViewController{
    CGFloat descHeight;
    CGFloat estimateHeight;
    CGFloat predicateHeight;
    CGFloat adjustMeasureHeight;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"矛盾纠纷详情";
    [self loadData];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)loadData{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]problemDetailWithPid:self.detail_id stepId:self.stepId stepStatusCode:self.statusCode successBlock:^(BaseModel *model) {
        [self endLoading];
        ProblemDetailModel *m = (ProblemDetailModel *)model;
        if (m.success) {
            self.eventNameLabel.text = m.event_name;
            self.orgNameLabel.text = m.orgName;
            self.occurPlaceLabel.text = m.occur_place;
            self.occurDateLabel.text = m.occur_time;
            self.relatedCompaynLabel.text = m.related_company;
            self.relatedPersonCountLabel.text = m.realted_count;
            self.manipulatorLabel.text = m.mainCharacters;
            self.eventTypeLabel.text = m.event_type;
            self.eventScaleLabel.text = m.event_scale;
            self.eventSourceLabel.text = m.event_source;
            self.eventDescLabel.text = m.event_desc;
            descHeight = [self.eventDescLabel.text caculateSizeWithFont:[UIFont systemFontOfSize:14] MaxSize:CGSizeMake(self.view.width - 120, CGFLOAT_MAX)].height + 16;
            self.suggestionLabel.text = m.evaluateOpition;
            estimateHeight = [self.suggestionLabel.text caculateSizeWithFont:[UIFont systemFontOfSize:14] MaxSize:CGSizeMake(self.view.width - 120, CGFLOAT_MAX)].height + 16;
            self.forecastLabel.text = m.developForecast;
            predicateHeight = [self.forecastLabel.text caculateSizeWithFont:[UIFont systemFontOfSize:14] MaxSize:CGSizeMake(self.view.width - 120, CGFLOAT_MAX)].height + 16;
            self.adjustMeasuresLabel.text = m.adjustMeasures;
            adjustMeasureHeight = [self.adjustMeasuresLabel.text caculateSizeWithFont:[UIFont systemFontOfSize:14] MaxSize:CGSizeMake(self.view.width - 120, CGFLOAT_MAX)].height + 16;
            self.files = m.files;
            if ([m.files count] > 0) {
                UIView *footer = [[UIView alloc]init];
                footer.backgroundColor = [UIColor whiteColor];
                CGFloat width = (ScreenWidth - 48) / 3;
                [m.files enumerateObjectsUsingBlock:^(FileItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(16 + (width + 16) * idx, 16, width, width)];
                    imageView.contentMode = UIViewContentModeScaleToFill;
                    imageView.tag = idx;
                    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(show:)];
                    if (obj.type == Document) {
                        //文档类型
                        imageView.image = [UIImage imageNamed:@"icon_file"];
                    }else if (obj.type == Image){
                        [imageView sd_setImageWithURL:[NSURL URLWithString:obj.path]];
                    }else if (obj.type == Video){
                        imageView.image = [UIImage imageNamed:@"icon_video"];
                    }else if (obj.type == Audio){
                        imageView.image = [UIImage imageNamed:@"icon_audio"];
                    }
                    imageView.userInteractionEnabled = YES;
                    [imageView addGestureRecognizer:tap];
                    [footer addSubview:imageView];
                }];
                footer.frame = CGRectMake(0, 0, self.view.width, width + 32);
                self.tableView.tableFooterView = footer;
                [self.tableView reloadData];
            }
        }else{
            [self alertSome:m.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)showDocumentWithURL:(NSString *)url{
    
}

-(void)showImageWithURL:(UIImageView *)view{
    NSMutableArray *browseItemArray = [[NSMutableArray alloc]init];
    UIImage *image = view.image;
    if (image.size.width > 0) {
        MSSBrowseModel *browseItem = [[MSSBrowseModel alloc]init];
        browseItem.bigImage = image;
        browseItem.smallImageView = view;
        [browseItemArray addObject:browseItem];
    }
    
    MSSBrowseLocalViewController *bvc = [[MSSBrowseLocalViewController alloc]initWithBrowseItemArray:browseItemArray currentIndex:0];
    bvc.presentVC = self.navigationController;
    [bvc showBrowseViewController];
}

-(void)showVideoWithURL:(NSString *)url{
    AVPlayerViewController *VC = [[AVPlayerViewController alloc]init];
    VC.player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:url]];
    VC.videoGravity = AVLayerVideoGravityResizeAspect;
    [self.navigationController presentViewController:VC animated:NO completion:nil];
}

-(void)showAudioWithURL:(NSString *)url{
    AVPlayerViewController *VC = [[AVPlayerViewController alloc]init];
    VC.player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:url]];
    VC.videoGravity = AVLayerVideoGravityResizeAspect;
    [self.navigationController presentViewController:VC animated:NO completion:nil];
}

-(void)show:(UITapGestureRecognizer *)tap{
    UIImageView *imageView = (UIImageView *)tap.view;
    FileItemModel *model = self.files[imageView.tag];
    switch (model.type) {
        case Document:
            [self showDocumentWithURL:[NSString stringWithFormat:@"%@/%@",ServerPath,model.path]];
            break;
        case Image:
            [self showImageWithURL:imageView];
            break;
        case Video:
            [self showVideoWithURL:[NSString stringWithFormat:@"%@/%@",ServerPath,model.path]];
            break;
        case Audio:
            [self showAudioWithURL:[NSString stringWithFormat:@"%@/%@",ServerPath,model.path]];
            break;
        default:
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < 10) {
        return 44.0;
    }else if (indexPath.row == 10){
        return descHeight > 44 ? descHeight : 44;
    }else if (indexPath.row == 11){
        return estimateHeight > 44 ? estimateHeight : 44;
    }else if (indexPath.row == 12){
        return predicateHeight > 44 ? predicateHeight : 44;
    }else if (indexPath.row == 13){
        return adjustMeasureHeight > 44 ? adjustMeasureHeight : 44;
    }else{
        return 0.0;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
